package cn.tedu.kcircle.attachment.adapter.converter;

import cn.tedu.kcircle.attachment.protocol.dto.UploadRequestDTO;
import cn.tedu.kcircle.attachment.protocol.dto.UploadResultDTO;
import cn.tedu.kcircle.attachment.protocol.param.UploadRequestParam;
import cn.tedu.kcircle.attachment.repository.model.AttachAddModel;
import cn.tedu.kcircle.attachment.repository.model.AttachLogAddModel;
import cn.tedu.kcircle.attachment.service.bo.UploadRequestBO;
import cn.tedu.kcircle.attachment.service.bo.UploadResultBO;
import cn.tedu.kcircle.attachment.service.bo.UploadResultItemBO;
import cn.tedu.kcircle.common.enums.FileType;
import cn.tedu.kcircle.common.po.CurrentPrincipal;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
<<<<<<< HEAD
import java.util.Objects;
=======
>>>>>>> refs/remotes/origin/micro-service

@Component
public class AttachmentAdapterConverter {
    /*封装上传返回结果BO对象*/
    public UploadResultBO bo2BO(UploadRequestBO uploadRequestBO, List<String> attachFiles) {
        MultipartFile[] fileList = uploadRequestBO.getFileList();
        int[] count = {0};
        List<UploadResultItemBO> uploadResultItemBOS = attachFiles.stream().map((file) -> {
            UploadResultItemBO uploadResultItemBO = new UploadResultItemBO();
            uploadResultItemBO.setContentType(fileList[count[0]].getContentType());
            uploadResultItemBO.setContentLength(fileList[count[0]].getSize());
            uploadResultItemBO.setClientFileName(fileList[count[0]].getOriginalFilename());
            uploadResultItemBO.setAttachFileName(file);
            count[0]++;
            return uploadResultItemBO;
        }).toList();
        UploadResultBO uploadResultBO = new UploadResultBO();
        uploadResultBO.setUploadResult(uploadResultItemBOS);
        return uploadResultBO;
    }

    /*封装上传业务请求BO对象*/
    public UploadRequestBO param2BO(UploadRequestParam uploadRequestParam, Integer resourceType, CurrentPrincipal principal) {
        MultipartFile[] fileList = uploadRequestParam.getFile();
        UploadRequestBO uploadRequestBO = new UploadRequestBO();
        uploadRequestBO.setUserId(principal.getUserId());
        uploadRequestBO.setUsername(principal.getUsername());
        uploadRequestBO.setRemark(uploadRequestParam.getRemark());
        uploadRequestBO.setResourceType(resourceType);
        uploadRequestBO.setResourceId(uploadRequestParam.getResourceId());
        uploadRequestBO.setFileList(fileList);
        return uploadRequestBO;
    }

    /*封装远程调用上传业务BO对象*/
    public UploadRequestBO dto2BO(UploadRequestDTO uploadRequestDTO){
        UploadRequestBO uploadRequestBO = new UploadRequestBO();
        BeanUtils.copyProperties(uploadRequestDTO,uploadRequestBO);
        return uploadRequestBO;
    }
    /*封装远程调用上传结果DTO对象*/
    public UploadResultDTO bo2DTO(UploadResultBO uploadResultBO){
        UploadResultDTO uploadResultDTO = new UploadResultDTO();
        BeanUtils.copyProperties(uploadResultBO,uploadResultDTO);
        return uploadResultDTO;
    }
    /*封装上传结果保存attach记录Model对象*/
    public List<AttachAddModel> bo2Models(UploadRequestBO uploadRequestBO,UploadResultBO uploadResultBO){
        MultipartFile[] fileList = uploadRequestBO.getFileList();
        int[] count={0};
        return uploadResultBO.getUploadResult().stream().map((result)-> {
            AttachAddModel attachAddModel = new AttachAddModel();
            attachAddModel.setCreateUserId(uploadRequestBO.getUserId());
            attachAddModel.setCreateUserName(uploadRequestBO.getUsername());
            attachAddModel.setModifiedUserId(uploadRequestBO.getUserId());
            attachAddModel.setModifiedUserName(uploadRequestBO.getUsername());
            attachAddModel.setResourceType(uploadRequestBO.getResourceType());
            attachAddModel.setResourceId(uploadRequestBO.getResourceId());
            attachAddModel.setRemark(uploadRequestBO.getRemark());
            BeanUtils.copyProperties(result, attachAddModel,"contentType");
            Integer fileType = getFileType(fileList[count[0]]);
            if(Objects.equals(fileType, FileType.IMAGE.getType())){
                BufferedImage bufferedImage = null;
                try {
                    bufferedImage = ImageIO.read(fileList[count[0]++].getInputStream());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                attachAddModel.setWidth(bufferedImage.getWidth());
                attachAddModel.setHeight(bufferedImage.getHeight());
            }
//            if(Objects.equals(fileType,FileType.VIDEO.getType())){
//                new MultimediaInfo
//            }
            attachAddModel.setContentType(getFileType(fileList[count[0]]));
            return attachAddModel;
        }).toList();
    }
    /*封装上传业务日志记录Model对象*/
    public List<AttachLogAddModel> bo2Models(UploadRequestBO uploadRequestBO,UploadResultBO uploadResultBO,Integer opEventType,List<Long> attachIds){
        MultipartFile[] fileList = uploadRequestBO.getFileList();
        int[] count={0};
        return uploadResultBO.getUploadResult().stream().map((uploadResultItemBO -> {
            AttachLogAddModel attachLogAddModel = new AttachLogAddModel();
            attachLogAddModel.setOpUserId(uploadRequestBO.getUserId());
            attachLogAddModel.setOpUserName(uploadRequestBO.getUsername());
            attachLogAddModel.setOpEventType(opEventType);
            attachLogAddModel.setClientFileName(uploadResultItemBO.getClientFileName());
            attachLogAddModel.setAttachFileName(uploadResultItemBO.getAttachFileName());
            attachLogAddModel.setAttachId(attachIds.get(count[0]));
            attachLogAddModel.setContentType(getFileType(fileList[count[0]++]));
            attachLogAddModel.setRemark(uploadRequestBO.getRemark());
            return attachLogAddModel;
        })).toList();
    }

    /*根据名称获取文件类型*/
    private Integer getFileType(MultipartFile file) {
        Integer fileType = FileType.UNKNOWN.getType();
        if (file.getContentType().contains("video")) {
            fileType = FileType.VIDEO.getType();
        } else if (file.getContentType().contains("image")) {
            fileType = FileType.IMAGE.getType();
        }
        return fileType;
    }
}
